var account = '${session.account.name!}';
// 刷新验证码
function doRefCode(){
	$("#getCode").attr("src","/getCode?"+Math.random());
}
$(document).keydown(function (e){
	if (e.keyCode == 13){
		loginSubmit();
	}
});	
//登录模块，默认隐藏登录
$('.login').hide();
//显示登录页面
function showLogin(){
	$("#getCode").attr("src","/getCode?"+Math.random());
	$('.login').fadeIn("slow");
}
//隐藏登录页面
function hideLogin(){
	$('.login').fadeOut("slow");
}


//登录方法
function loginSubmit(){
	var name = $('#name').val();
	var password = $('#password').val();
	var code = $('#code').val();
	if(name == ''){
		alert('请输入帐户');
		return;
	}
	if(password == ''){
		alert('请输入密码');
		return;
	}
	if(code == ''){
		alert('请输入验证码，查看下一张请点击验证码图片刷新');
		return;
	}
	var remember = $('#remember').val();
	$.ajax({
		type: 'post',
		data: {'name':name,'password':password,'code':code,'remember':remember},
		dataType: 'json',
		asycn: true,
		url: '/loginSubmit',
		success: function(data, status){
			if(data.msg == 'success'){
				//刷新当前页面
				//window.parent.location.reload();
				window.parent.location = window.parent.location.href;
			}else{
				doRefCode();
				$('#code').val('');
				$('#password').val('');
				$('#error').html(data.msg);
			}
		},
		error: function(XMLHttpRequest, textStatus, thrownError){
			alert(textStatus);
		}
	}); 
}

/**添加购物车*/
function addToCart(cuisine_dish_id){
	if(account == ''){
		showLogin();
		return;
	}
	$.ajax({
		url: '/business/cart/addToCart',
		data: {'cuisine_dish_id': cuisine_dish_id},
		dataType: 'json',
		success: function(data){
			if(data.msg = "true"){
				alert('添加成功');
			}else if(data.msg == 'notOnSell'){
				alert('添加失败，已经售完了');
				windows.location = reload();
			}else{
				alert('添加失败');
			}
		},
		error: function(){
			alert("添加出错了");
			showLogin();
		}
	});
}
/**下单*/
function toOrder(){
	if(account == ''){
		showLogin();
		return false;
	}
	return true;
}

/*二维码*/
$('.showerweima').click(function(){
	$('.erweima').fadeIn('slow');
	$("#erweima").qrcode({ 
	    render: "table", //table方式 
	    width: 200, //宽度  
	    height: 200, //高度 
	    text: "http://192.168.191.1/"
	    //text: "http://10.50.43.60/" //任意内容 
	});
});
$('.erweima').click(function(){
	$('.erweima').fadeOut('slow');
	$("#erweima").html('');
});